
var data2=null
$('.back1').on('click',function(){
  window.history.go(-1)
})
 {if $Think.get.car_id}
 Load();
 data2={};
 var where_json ={};
 where_json.car_id=['in','{$Think.get.car_id}']
 data2.where_json=JSON.stringify(where_json)
 ajax(api.car.list,data2,'post').done((res)=> {
    Load(1)
    if(res.code==1){
        res= carinfo(res)
        $('.choose').on('click',function(){
          var index = parent.layer.getFrameIndex(window.name);
							parent.$('.car_id').val(res.list[0].car_id)
							parent.$('.number').val(res.list[0].number)
							parent.layer.close(index);
        })
        for(var key in res.list[0]){
          if(key == 'driving_license_img'|| key == 'driving_license_img2' || key =='front_img'|| key =='back_img' || key =='side_img' ||key =='engine_img'||key =='business_policy_img' ||key =='compulsory_policy_img'){
            if(res.list[0][`${key}`]){
              $(`.${key}`).attr('src', domain+'/'+ res.list[0][`${key}`])
              // console.log(res.list[0][`${key}`])
            }else{
              $(`.${key}`).attr('src', '/assets/img/noimg.gif')
            }
          }
          
          if(res.list[0][key]== null ){
            $('input[name='+key+']').val('暂无信息')
          }else if(res.list[0][key]===''){
            $('input[name='+key+']').val('暂无信息')
          }else{
            $('input[name='+key+']').val(res.list[0][key])
          }
          if(key =='company'){
            if(res.list[0][key].company_name){
            $('input[name=company_name]').val(res.list[0][key].company_name)
            }else{
            $('input[name=company_name]').val('暂无信息')
            }
          }else if (key == 'cartype'){
            if(res.list[0][key].car_type_name){
            $('input[name=car_type_name]').val(res.list[0][key].car_type_name)
            }else{
            $('input[name=car_type_name]').val('暂无信息')
            }
          }else if(key=='entrust'){
            if(res.list[0][key].company_name){
            $('input[name=entrust_company_name]').val(res.list[0][key].company_name)    

            }else{
            $('input[name=entrust_company_name]').val('暂无信息')    

            }
          }
      }
    }
 }).fail((err)=>{
    Load(1)
    console.log(err)
    Err('请求失败')
 })
 {else/}

{/if}
$('.car_check').on('click',function(){
	Load()
	ajax(api.cycle_buy.check,{
		car_audit_id:"{$Think.get.audit}",
		state:5
	},'post').done((res)=>{
		Load(1)
		if(res.code==1){
			Msg('审核通过')
			window.location.href=path.car.buy_index
		}else{
			Err(res.msg)
			
		}
		layer.close(auto)
	}).fail(()=>{
		Load(1)
		Err('请求失败')
		layer.close(auto)
	})
})
layui.use('element', function () {
    var element = layui.element;
   
    //一些事件监听
    element.on('tab(demo)', function (data) {
    var data3=null;
    Load();
    data3={};
    var where_json = {"car.car_id":["in",'{$Think.get.car_id}']}
    data3.where_json=JSON.stringify(where_json)
      console.log(data.index)
      if(data.index==0){
        Load(1)
      }
      if (data.index == 1) {  
        if(data3){  
        Load();
        ajax(api.service.list,data3,'post').done(function(res) {
            console.log(res)
            Load(1);
            // 成功回调
              table.render({
                elem: '#service'
                ,data:res.data
                ,cellMinWidth: 80
                ,page:{theme: '#1E9FFF'}
                ,cols: [[
                  {field:'start_date',  title: '维保开始时间'}
                  ,{field:'end_date',  title: '维保结束时间'}
                  ,{field:'price',  title: '维保金额'}
                  ,{field:'address',  title: '维保地点'}
                  ,{field:'leader',  title: '维保负责人'}
                  ,{field:'service_type',  title: '维保项目',templet:'<div>{{d.service_type==1?"维修":"保养"}}</div>'}
                  ,{field:'remind',  title: '维保提醒',templet:'<div>{{d.remind?d.remind:"无提醒信息"}}</div>'}
                  ,{ title: '操作',width:110,align:'center', toolbar:'#demo',fixed: 'right'}
                ]]
              });
               // 操作
        table.on('tool(demo)', function (obj) {
          var data1 = obj.data; //获得当前行数据
          var datas ={};
          var where_json ={};
          where_json.maintenance_record_id=['in',data1.maintenance_record_id]
          datas.where_json=JSON.stringify(where_json)
        
          var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
          console.log('caizuo')
          if (layEvent === 'detail') {      
                      ajax(api.service.list, datas, 'post').done(function (res) {
                          // 成功回调
                          console.log(res)
                          if(res.code == 1){
                            var key=['维保单号','车牌号','维保类型','维保金额','送维/送保人','维保公司','开始时间','结束时间','维保公司联系人','维保公司联系电话','地点','备注说明'];
                            var val=['maintenance_record_id','car_id','service_type','price','leader','service_company','start_date','end_date','service_linkman','service_phone','address','ps'];
                            var detail= res.data[0];                         
                            layer.open({
                              type: 1 //Page层类型
                              ,area: ['500px', '700px']
                              ,title: '维保详情'
                              ,shade: 0.6 //遮罩透明度
                              ,anim: 1 //0-6的动画形式，-1不开启
                              ,content:`${contents(detail,key,val)}`
                            }); 
                          }else{
                            Err("请求失败")
                          }
                           
                      }).fail(function (err) {
                          // 失败回调
                          Err(res.msg ? res.msg : '操作失败，稍候再试');
                      });
                         
              }
          })
            }).fail(function(err) {
            // 失败回调
            Load(1)
            console.log(err)
            Err('请求失败')
            });
          }else{
            Err('无数据')
          }
      } else if (data.index == 2) {
        Load(1);
        var data=[
          {id:'2018-04-15',username:'97',sex:'123'},
          {id:'2018-07-21',username:'92',sex:'555'},
          {id:'2018-02-13',username:'95',sex:'222'},
          {id:'2018-05-14',username:'92',sex:'554'},
        ]
        table.render({
          elem: '#refuel'
          ,data:data
          ,cellMinWidth: 80
          ,page:{theme: '#1E9FFF'}
          ,cols: [[
            {field:'id',  title: '加油日期'}
            ,{field:'username',  title: '加油类型'}
            ,{field:'sex',  title: '费用'}
          ]]
        });
      }else if(data.index == 3 ){
        if(data3){  
          Load();
          ajax(api.insurance.list,data3,'post').done(function(res) {
              console.log(res)
              Load(1);
              // 成功回调
                table.render({
                  elem: '#insurance'
                  ,data:res.data
                  ,cellMinWidth: 80
                  ,page:{theme: '#1E9FFF'}
                  ,cols: [[
                    {field: 'policy_number', title: '保险号',minWidth:210}
                    ,{field: 'policy_holder', title: '投保人'}  
                    ,{field: 'policy_type', title: '保险种类',templet:'<div>{{d.policy_type==1?"商业险":"交通强制险"}}</div>'}
                    ,{field:'service_id', title:'保险公司',templet:'<div>{{d.service.service_name?d.service.service_name:"无"}}</div>'}
                    ,{field:'policy_charge', title:'保险费用'}
                    ,{field:'start_date', title:'投保时间'}
                    ,{field:'end_date', title:'到期时间'}
                    ,{field:'ps', title:'备注说明',templet:'<div>{{d.ps?d.ps:"无"}}</div>'}
                    ,{ title: '操作',width:110,align:'center', toolbar:'#demo',fixed: 'right'}
                  ]]
                });
                 // 操作
          table.on('tool(demo)', function (obj) {
            var data1 = obj.data; //获得当前行数据
            var datas ={};
            var where_json ={};
            where_json.id=['in',data1.id]
            datas.where_json=JSON.stringify(where_json)
          
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            console.log('caizuo')
            if (layEvent === 'detail') {      
                        ajax(api.insurance.list, datas, 'post').done(function (res) {
                            // 成功回调
                            console.log(res)
                            if(res.code == 1){
                              var key=['保单号','车牌号','保险种类','购买金额','保险公司','投保人','保险公司联系人','保险公司联系电话','投保时间','到期时间','备注说明'];
					                    var val=['policy_number','car','policy_type','policy_charge','service_name','policy_holder','link_man','link_phone','start_date','end_date','ps']
                              var detail= res.data[0];                              
                              layer.open({
                                type: 1 //Page层类型
                                ,area: ['500px', '700px']
                                ,title: '维保详情'
                                ,shade: 0.6 //遮罩透明度
                                ,anim: 1 //0-6的动画形式，-1不开启
                                ,content:`${contents(detail,key,val)}`
                              }); 
                            }else{
                              Err("请求失败")
                            }
                             
                        }).fail(function (err) {
                            // 失败回调
                            Err(res.msg ? res.msg : '操作失败，稍候再试');
                        });
                           
                }
            })
              }).fail(function(err) {
              // 失败回调
              Load(1)
              console.log(err)
              Err('请求失败')
              });
            }else{
              Err('无数据')
            }
          //年检
      }else if(data.index == 4 ){
        if(data3){  
          Load();
          ajax(api.AS.list,data3,'post').done(function(res) {
              console.log(res)
              Load(1);
              // 成功回调
                table.render({
                  elem: '#AS'
                  ,data:res.data
                  ,cellMinWidth: 80
                  ,page:{theme: '#1E9FFF'}
                  ,cols: [[
                    {field:'id',  title: '年检编号'}
                    ,{field:'start_date',  title: '年检日期'}
                    ,{field:'service_id',  title: '年检地点'}
                    ,{field:'checker',  title: '年检人'}
                    ,{field:'state',  title: '年检状态',templet:'<div>{{d.state==1?"通过":"未通过"}}</div>'}
                    ,{field:'price',  title: '年检费用',}
                    ,{ title: '操作',width:110,align:'center', toolbar:'#demo',fixed: 'right'}
                  ]]
                });
                 // 操作
          table.on('tool(demo)', function (obj) {
            var data1 = obj.data; //获得当前行数据
            var datas ={};
            var where_json ={};
            where_json.id=['in',data1.id]
            datas.where_json=JSON.stringify(where_json)
          
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            if (layEvent === 'detail') {      
                        ajax(api.AS.list, datas, 'post').done(function (res) {
                            // 成功回调
                            console.log(res)
                            if(res.code == 1){
                              var key=['车牌号','年检地址','年检时间','过期时间','送检人','年检费用','检验员','年检状态','备注说明'];
                              var val=['car','service_id','start_date','end_date','sender','price','checker','state','remark'];
                              var detail= res.data[0];
                              
                              layer.open({
                                type: 1 //Page层类型
                                ,area: ['500px', '700px']
                                ,title: '维保详情'
                                ,shade: 0.6 //遮罩透明度
                                ,anim: 1 //0-6的动画形式，-1不开启
                                ,content:`${contents(detail,key,val)}`
                              }); 
                            }else{
                              Err("请求失败")
                            }
                             
                        }).fail(function (err) {
                            // 失败回调
                            Err(res.msg ? res.msg : '操作失败，稍候再试');
                        });
                           
                }
            })
              }).fail(function(err) {
              // 失败回调
              Load(1)
              console.log(err)
              Err('请求失败')
              });
            }else{
              Err('无数据')
            }
      }
    });
  });

// 详情内容
  function contents(detail,key,val){
    var content ='<div style="padding:20px; margin:0 atuo">';
    for(var i = 0; i < key.length;i++){
      if(val[i] == 'car'){
        content +=  `<div class="layui-form-item" style="margin:0 auto;width:90%">
        <label class="layui-form-label" style="width:150px">${key[i]}：</label>
        <div class="layui-input-inline">
            <input type="text" name="maintenance_record_id" value="${detail[val[i]].number}" class="layui-input border-none" readonly='readonly'>
        </div>
        </div>`
      }else if(val[i] == 'service_type'){
        content +=  `<div class="layui-form-item" style="margin:0 auto;width:90%">
        <label class="layui-form-label" style="width:150px">${key[i]}：</label>
        <div class="layui-input-inline">
            <input type="text" name="maintenance_record_id" value="${detail[val[i]]==1?'维修':'保养'}" class="layui-input border-none" readonly='readonly'>
        </div>
        </div>`
      }else if(val[i] == 'state'){
        content +=  `<div class="layui-form-item" style="margin:0 auto;width:90%">
        <label class="layui-form-label" style="width:150px">${key[i]}：</label>
        <div class="layui-input-inline">
            <input type="text" name="maintenance_record_id" value="${detail[val[i]]==1?'通过':'未通过'}" class="layui-input border-none" readonly='readonly'>
        </div>
        </div>`
      }else if(val[i] == 'policy_type'){
        content +=  `<div class="layui-form-item" style="margin:0 auto;width:90%">
        <label class="layui-form-label" style="width:150px">${key[i]}：</label>
        <div class="layui-input-inline">
            <input type="text" name="maintenance_record_id" value="${detail[val[i]]==1?'交通强制险':'商业险'}" class="layui-input border-none" readonly='readonly'>
        </div>
        </div>`
      }else if(val[i] == 'service_name'||val[i] == 'link_man' ||val[i] == 'link_phone'){
        content +=  `<div class="layui-form-item" style="margin:0 auto;width:90%">
        <label class="layui-form-label" style="width:150px">${key[i]}：</label>
        <div class="layui-input-inline">
            <input type="text" name="maintenance_record_id" value="${detail['service'][val[i]] ?detail['service'][val[i]] :'暂无信息'}" class="layui-input border-none" readonly='readonly'>
        </div>
        </div>`
      }else{
        content +=  `<div class="layui-form-item" style="margin:0 auto;width:90%">
        <label class="layui-form-label" style="width:150px">${key[i]}：</label>
        <div class="layui-input-inline">
            <input type="text" name="maintenance_record_id" value="${detail[val[i]]?detail[val[i]]:'暂无信息'}" class="layui-input border-none" readonly='readonly'>
        </div>
        </div>`
      }
    }
    content +="</div>";
    return content
  }